package com.pachong.frameworkdemo.module1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cncn.youmengsharelib.ShareData;
import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;
import com.cncn.youmengsharelib.ui.DemoSharePlatformsFragment;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.frameworkdemo.R;
import com.pachong.frameworkdemo.account.UrlCenter;
import com.pachong.frameworkdemo.common.http.MyHttpRequest;
import com.pachong.frameworkdemo.common.http.TokenInfo;
import com.pachong.frameworkdemo.common.web.CommonWebViewActivity;
import com.pachong.frameworkdemo.demo.httpdemo.HttpDemoIndexActivity;

import java.io.File;
import java.io.Serializable;

import butterknife.OnClick;

@SuppressLint("ValidFragment")
public class Main1Fragment extends BaseFragment implements Serializable {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        getCustomToolbar().setTitle("fragment center title");
        return view;
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main1, null);
        return view;
    }


    @OnClick(R.id.btnShare1)
    void onShareClicked() {
        // Warning： 注意：在调用的Activity中配置 onActivityResult代码
/*
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
            UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
            if(ssoHandler != null){
                ssoHandler.authorizeCallBack(requestCode, resultCode, data);
            }

        }
        */

        String imageUrl = "http://pic24.nipic.com/20121025/11197132_095434613172_2.jpg";
        String targetUrl = "http://pic24.nipic.com/20121025/11197132_095434613172_2.jpg";
        ShareData shareInfo = new ShareData(imageUrl, "TiTLE", "content", targetUrl);

        DemoSharePlatformsFragment shareFrag = DemoSharePlatformsFragment.newInstance(
                shareInfo.getTitle(),
                shareInfo.getContent(),
                shareInfo.getImageUrl(),
                shareInfo.getTargetUrl());
        shareFrag.setShareListener(new ShareListener() {
            @Override
            public void onShareSuccess(Activity shareActivity, SharePlatform platform) {
                EasyToast.showToast(getActivity(), "test onShareSuccess");
            }

            @Override
            public void onShareFailed(Activity shareActivity, SharePlatform platform) {
                EasyToast.showToast(getActivity(), "test onShareFailed");
            }

            @Override
            public void onShareCancel(Activity shareActivity, SharePlatform platform) {
                EasyToast.showToast(getActivity(), "test onShareCancel");
            }
        });
        shareFrag.setStyle(DialogFragment.STYLE_NO_TITLE, com.cncn.youmengsharelib.R.style.ShareDialogStyle);
        shareFrag.show(getChildFragmentManager(), "");

    }


    @OnClick(R.id.btnHttp)
    void onHttpReqClicked() {
        Intent intent = new Intent(getActivity(), HttpDemoIndexActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btnWebview)
    void onWebviewClicked() {
        CommonWebViewActivity.start(getActivity(), "https://console.easemob.com/app_list.html");

       /* Intent intent = new Intent(getActivity(), CommonWebViewActivity.class);
        startActivity(intent);*/
    }

    @OnClick(R.id.btnWebPickPic)
    void onBtnWebPickPicClicked() {
       /* Intent intent = new Intent(getActivity(), CommonWebViewActivity.class);
        startActivity(intent);*/
    }

    @OnClick(R.id.btnUploadPhoto)
    void onUploadPhotoClicked() {
        String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
//        String path = sdcard + "DCIM/YiCamera/aaa/55555.jpg";
        String path = sdcard + "DCIM/Screenshots/test.png";
        File file = new File(path);
        Log.e("vivi", "  path ==  " + path);
        Log.e("vivi", "  file exist  ==  " + file.exists());

        UrlParams urlParams = new UrlParams();
        // 方法一
        urlParams.put(new UrlParams.FileWrapper("file", file, file.getName(), "image/png"));
        urlParams.put("file", file);

        // 方法二
        IHttpRequest request = new MyHttpRequest(getActivity());
        request.post(UrlCenter.UPLOAD_PORTRAIT, urlParams, new DataRequestListener<TokenInfo>(TokenInfo.class) {

            @Override
            public void onSuccess(TokenInfo bean) {
                Log.e("vivi", "onSuccess  bean == " + bean);
                super.onSuccess(bean);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Log.e("vivi", "onFailure  str msg " + strMsg + " errorNo == " + errorNo);
                super.onFailure(t, errorNo, strMsg);
            }
        });

        /*
         UrlParams urlParams = new UrlParams();
        urlParams.put(new UrlParams.FileWrapper("file", file, file.getName(), "image/png"));
        urlParams.put("file", file);

        MultipartPostRequest mRequest = new MultipartPostRequest(UrlCenter.UPLOAD_PORTRAIT, urlParams, null, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.e("vivi", "onResponse response = " + response);

            }


        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("vivi", "onErrorResponse ");

                String errorMsg = "";
                if (error != null && error.networkResponse != null && error.networkResponse.data != null) {
                    errorMsg = new String(error.networkResponse.data);
                }
                int errorNo = -1;
                if (error != null && error.networkResponse != null) {
                    errorNo = error.networkResponse.statusCode;
                }
                // 没有网络
                if (errorNo == -1 && TextUtils.isEmpty(errorMsg)) {
                    errorMsg = "网络不可用，请检查网络";
                }
                Log.e("vivi", "  error msg ==   " + errorMsg);
            }
        });
        QueueHelper.getInstance().addQueue(getActivity(), mRequest);*/
    }


}
